//
//  RBTabBar.m
//  Budejie
//
//  Created by 华杨科技 on 2017/9/19.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "RBTabBar.h"
@interface RBTabBar()
@property (nonatomic, weak) UIButton *publicButton; // weak 弱指针
@end

@implementation RBTabBar

#pragma mark - 懒加载
- (UIButton *)publicButton {

    if (!_publicButton) {
        // 创建对象btn
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(clickPublicAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn]; // 添加到 tabBar上,tabBar不死,btn 就不会死
        
        // 用一个新指针指向btn;
        _publicButton = btn;
    }
    return _publicButton;
}

#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置tabbar 背景图片
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
    }
    return self;
}

/**
 布局子控件
 */
- (void)layoutSubviews {
    [super layoutSubviews];

    //    NSClassFromString(@"UITabBarButton") == [UITabBarButton class];
    //    NSClassFromString(@"UIButton") == [UIButton class];

    /**** 设置所有tabBarButton按钮的frame*****/
    // 按钮的尺寸
    CGFloat tabBarButtonW = self.frame.size.width / 5;
    CGFloat tabBarButtonH = self.frame.size.height;
    CGFloat tabBarButtonX = 0;
    CGFloat tabBarButtonY = 0;
    
    // tabbar下标
    int index = 0;
    
    /*** 设置tabBar的frame***/
    for (UIView *subview in self.subviews) {
        
        // 过滤非tabbarButton控件
//        if (subview.class != NSClassFromString(@"UITabBarButton")) continue;
        if (![@"UITabBarButton" isEqualToString:NSStringFromClass(subview.class)]) continue;
        
        tabBarButtonX = index * tabBarButtonW; 
        if (index >= 2) { // 右边的tabBar
            tabBarButtonX += tabBarButtonW;
        }
//        subview.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
        subview.rb_width = tabBarButtonW;
        subview.rb_height = tabBarButtonH;
        subview.rb_x = tabBarButtonX;
        subview.rb_y = tabBarButtonY;
        index++;
    }
    
    /*** 添加中间发布按钮***/
//    self.publicButton.frame = CGRectMake(0, 0, tabBarButtonW, tabBarButtonH);
    self.publicButton.rb_width = tabBarButtonW;
    self.publicButton.rb_height = tabBarButtonH;
//    self.publicButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    self.publicButton.rb_center_x = self.rb_width * 0.5;
    self.publicButton.rb_center_y = self.rb_height * 0.5;
    
}

#pragma mark - 监听
- (void)clickPublicAction {
    RBNSLogFunction
}
@end
